package io.bluerain.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by foredawn on 15-8-26.
 * 反射调用方法
 */
public class ReflectSetter {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

        User u = new User();
        invokeSetter(u, "name", "呵呵哒");
        System.out.println(u.getName());
    }

    public static void invokeSetter(Object obj, String field, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        field = "set" + field.substring(0, 1).toUpperCase() + field.substring(1);
        Class<?> clazz = obj.getClass();
        Method method = clazz.getMethod(field, value.getClass());
        method.invoke(obj, value);

    }


}
